﻿//using Essensoft.Paylink.WeChatPay;
//using Essensoft.Paylink.WeChatPay.V2.Request;
//using Furion.DependencyInjection;
//using Furion.DynamicApiController;
//using Furion.FriendlyException;
//using HiSport.Application.Common.SqlSugar;
//using HiSport.Application.Service.Transaction.Refund.Dtos;
//using HiSport.Application.Service.Transaction.TransactionConfig.Service;
//using HiSport.Core.Entity.Order;
//using HiSport.Core.Entity.Pay;
//using HiSport.Core.Enum;
//using Microsoft.AspNetCore.Components.Forms;
//using Microsoft.AspNetCore.Mvc;
//using StackExchange.Redis;
//using System.ComponentModel;
//using static SKIT.FlurlHttpClient.Wechat.TenpayV3.Models.DepositMarketingMemberCardOpenCardCodesResponse.Types;

//namespace HiSport.Application.Service.Transaction.Refund.Service
//{

//    /// <summary>
//    /// 退款操作
//    /// </summary>
//    [ApiDescriptionSettings("交易信息", Tag = "退款", SplitCamelCase = false)]
//    public class RefundService : ITransient, IRefundService, IDynamicApiController
//    {

//        private readonly SqlSugarRepository<PayRecord> _payRecord;
//        private readonly SqlSugarRepository<RefundRecord> _refundRecord;
//        private readonly SqlSugarRepository<OrderSite> _orderSite;
//        private readonly SqlSugarRepository<OrderSiteInfo> _orderSiteInfo;
//        private readonly IHttpClientFactory _httpClientFactory;
//        private readonly Essensoft.Paylink.WeChatPay.V2.IWeChatPayClient _clientV2;
//        private readonly ITransactionConfigService _transactionConfigService;
//        public RefundService(SqlSugarRepository<PayRecord> payRecord,
//            SqlSugarRepository<OrderSite> orderSite,
//            ITransactionConfigService transactionConfigService,
//              Essensoft.Paylink.WeChatPay.V2.IWeChatPayClient clientV2,
//             SqlSugarRepository<RefundRecord> refundRecord,
//           SqlSugarRepository<OrderSiteInfo> orderSiteInfo)
//        {
//            _payRecord = payRecord;
//            _orderSite = orderSite;
//            _orderSiteInfo = orderSiteInfo;
//            _refundRecord = refundRecord;
//            _clientV2 = clientV2;
//            _transactionConfigService = transactionConfigService;
//        }


//        [ApiDescriptionSettings(Name = "OrderSiteRefund"), HttpPost]
//        [DisplayName("订单订单退款")]
//        public Task<bool> OrderSiteRefund(string orderNum)
//        {
//            var orderSite = _orderSite

//        }


//        [ApiDescriptionSettings(Name = "OrderSiteInfoRefund"), HttpPost]
//        [DisplayName("订场子订单退款")]
//        public async Task<bool> OrderSiteInfoRefund(long orderInfoId)
//        {
//            //查询订场子订单
//            var orderSiteInfo = await _orderSiteInfo.AsQueryable().Where(x => x.Id == orderInfoId).FirstAsync();
//            if (orderSiteInfo == null)
//            {
//                throw Oops.Oh("子订单不存在");
//            }
//            if (orderSiteInfo.ScheduledSiteState == ScheduledSiteStateEnum.Cancel)
//            {
//                throw Oops.Oh("子订单已被取消"); 
//            }


//            //订场订单
//            var orderSite = await _orderSite.AsQueryable().Where(x => x.Id == orderSiteInfo.OrderSiteId).FirstAsync();
//            if (orderSite == null)
//            {
//                throw Oops.Oh("订单不存在");
//            }
//            if (orderSite.OrderPayState == OrderPayStateEnum.Unpaid)
//            {
//                throw Oops.Oh("订单未支付");
//            }
//            if (orderSite.ActualPayAmount < orderSiteInfo.ActualPayAmount)
//            {
//                throw Oops.Oh("已支付金额小于订场金额");
//            }
//            if (orderSite.ActualPayAmount - orderSite.RefundAmount < orderSiteInfo.ActualPayAmount)
//            {
//                throw Oops.Oh("已支付金额不够订场金额");
//            }


//            //查询退款记录
//            var refundRecordList = _refundRecord.AsQueryable().Where(x => x.OrderId == orderSiteInfo.OrderSiteId && x.RefundState == RefundRecordStateEnum.RefundSuccess).ToList();

//            var payRecordids = refundRecordList.Select(x => x.PayRecordId).ToList();

//            //查询未退款的支付记录
//            var payRecordList = _payRecord.AsQueryable()
//                .Where(x => x.OrderId == orderSiteInfo.OrderSiteId
//                && x.PayState == PayRecordStateEnum.AllPayment
//                && payRecordids.Contains(x.Id)).ToList();

//            //查询 金额等于退款金额的支付记录
//            var thisPayRecord = payRecordList.Where(x => x.PayAmount == orderSiteInfo.ActualPayAmount).FirstOrDefault();


//            var list = new List<PayRecord>();

//            //满足直接退款
//            if (thisPayRecord != null)
//            {
//                list.Add(thisPayRecord);
//            }
//            else
//            {
//                //剩余需要退款的金额
//                var surplusAmount = orderSiteInfo.ActualPayAmount;
//                foreach (var payRecord in payRecordList)
//                {
//                    surplusAmount = surplusAmount - payRecord.PayAmount;
//                    if (surplusAmount <= 0)
//                    {
//                        break;
//                    }
//                    list.Add(payRecord);
//                }
//            }

//            //添加退记录  修改订单状态
//        }


//        /// <summary>
//        /// 微信退款
//        /// </summary>
//        /// <returns></returns>
//        [DisplayName("微信退款")]
//        [ApiDescriptionSettings(Name = "WeChatRefund"), HttpPost]
//        public async Task<RefundOutDto> WeChatRefund(RefundRecord inputDto, decimal totalAmount)
//        {
//            var result = new RefundOutDto()
//            {
//                PayNo = inputDto.PayRecordNo
//            };

//            //获取支付配置
//            var paySetting = await _transactionConfigService.GetDefaultPaySetting();


//            var request = new WeChatPayRefundRequest
//            {
//                OutRefundNo = inputDto.OutRefundNo,
//                TransactionId = inputDto.TransactionId,
//                OutTradeNo = inputDto.PayRecordNo,
//                TotalFee = (int)totalAmount * 100,
//                RefundFee = (int)inputDto.RefundAmount * 100,
//                RefundDesc = "订单退款",
//                NotifyUrl = ""
//            };
//            var response = await _clientV2.ExecuteAsync(request, paySetting.WeChatPay); ;

//            if (response.ReturnCode == Essensoft.Paylink.WeChatPay.V2.WeChatPayCode.Success)
//            {
//                if (response.ResultCode == Essensoft.Paylink.WeChatPay.V2.WeChatPayCode.Success)
//                {
//                    result.RefundState = RefundStateEnum.Success;
//                    result.Msg = "退款成功";

//                    inputDto.OutRefundNo = response.RefundId;
//                   var res=  await _refundRecord.InsertAsync(inputDto);
//                    if (res)
//                    {

//                    }
//                    else
//                    {
//                        result.RefundState = RefundStateEnum.Wait;
//                        result.Msg = "退款成功,记录失败待查询";
//                    }
//                }
//                else
//                {
//                    result.RefundState = RefundStateEnum.Fail;
//                    result.Msg = response.ErrCodeDes;
//                }
//            }
//            else
//            {
//                result.RefundState = RefundStateEnum.Fail;
//                result.Msg = response.ReturnMsg;
//            }

//        }

//        /// <summary>
//        /// 支付单号
//        /// </summary>
//        /// <param name="payRecordNo"></param>
//        /// <returns></returns>
//        public async Task<RefundOutDto> WeChatRefundQuery(string payRecordNo, WeChatPayOptions weChatPay)
//        {
//            var result = new RefundOutDto();

//            if (weChatPay == null)
//            {
//                //获取支付配置
//                var paySetting = await _transactionConfigService.GetDefaultPaySetting();
//                weChatPay = paySetting.WeChatPay;
//            }

//            //查询当前支付单的退款记录
//            var refundRecord = await _refundRecord.AsQueryable().Where(x => x.PayRecordNo == payRecordNo).FirstAsync();
//            result.PayNo = refundRecord.PayRecordNo;
//            if (refundRecord.RefundState == RefundRecordStateEnum.RefundSuccess)
//            {
//                result.RefundState = RefundStateEnum.Success;
//                result.Msg = "退款成功";
//                return result;
//            }

//            var request = new WeChatPayRefundQueryRequest
//            {
//                RefundId = refundRecord.OutRefundNo,
//                OutRefundNo = refundRecord.PayRecordNo,
//                TransactionId = refundRecord.TransactionId,
//                OutTradeNo = refundRecord.OrderNo
//            };
//            var response = await _clientV2.ExecuteAsync(request, weChatPay);

//            if (response.ReturnCode == Essensoft.Paylink.WeChatPay.V2.WeChatPayCode.Success)
//            {
//                if (response.ResultCode == Essensoft.Paylink.WeChatPay.V2.WeChatPayCode.Success)
//                {
//                    refundRecord.RefundState = RefundRecordStateEnum.RefundSuccess;
//                    var res=await _refundRecord.UpdateAsync(refundRecord);

//                    if (res)
//                    {

//                    }

//                }
//                else
//                {
//                    result.RefundState = RefundStateEnum.Fail;
//                    result.Msg = "退款付款失败" + response.ErrCodeDes;
//                }
//            }
//            else
//            {
//                result.RefundState = RefundStateEnum.Fail;
//                result.Msg = "退款失败"+ response.ErrCodeDes;
//            }
//            return result;
//        }

//        /// <summary>
//        /// 添加 退款记录  修改订单状态
//        /// </summary>
//        /// <param name=""></param>
//        /// <param name="payRecord"></param>
//        /// <param name="orderSiteInfo"></param>
//        /// <returns></returns>
//        private async Task<bool> AddRefundRecord(List<PayRecord> payRecord, OrderSite orderSite, OrderSiteInfo orderSiteInfo)
//        {
            

//        }
//    }
//}
